package com.easyoffer.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.hutool.core.bean.BeanUtil;
import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.EntryType;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.Tracer;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easyoffer.common.*;
import com.easyoffer.common.util.ResultUtils;
import com.easyoffer.model.entity.QuestionBank;
import com.easyoffer.sentinel.FallbackHandler;
import com.easyoffer.service.QuestionBankQuestionService;
import com.easyoffer.service.QuestionBankService;
import com.easyoffer.service.QuestionService;
import com.easyoffer.model.dto.question.QuestionQueryRequest;
import com.easyoffer.model.dto.questionbank.QuestionBankQueryRequest;
import com.easyoffer.model.dto.questionbank.QuestionBankUpdateRequest;
import com.easyoffer.model.dto.questionbank.QuestionBankAddRequest;
import com.easyoffer.model.vo.QuestionBankVO;
import com.easyoffer.model.vo.QuestionVO;
import com.jd.platform.hotkey.client.callback.JdHotKeyStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/questionBank")
public class QuestionBankController {
    @Autowired
    QuestionBankService questionBankService;
    @Autowired
    QuestionService questionService;
    @Autowired
    QuestionBankQuestionService questionBankQuestionService;
    @GetMapping("/get/vo")
    public BaseResponse<QuestionBankVO> getQuestionBankById(QuestionBankQueryRequest questionBankQueryRequest, HttpServletRequest request) {

        if (questionBankQueryRequest == null || questionBankQueryRequest.getId() == null)
            return ResultUtils.error(ErrorCode.PARAMS_ERROR);
        QuestionBank questionBank = questionBankService.getById(questionBankQueryRequest.getId());
        if (questionBank == null)
            return ResultUtils.success(null);
        //查询是否有缓存
        String key = "bank_" + questionBank.getId() ;
        if( questionBankQueryRequest.getIsGetQuestions() != null && questionBankQueryRequest.getIsGetQuestions()){
            key = "bank_with_pages_" + questionBank.getId();
        }
        if (JdHotKeyStore.isHotKey(key)) {
            if (JdHotKeyStore.get(key) != null ){
                return ResultUtils.success((QuestionBankVO) JdHotKeyStore.get(key));
            }
        }
        QuestionBankVO questionBankVO = new QuestionBankVO();
        BeanUtil.copyProperties(questionBank,questionBankVO);
        if (questionBankQueryRequest.getIsGetQuestions() != null && questionBankQueryRequest.getIsGetQuestions()){
            QuestionQueryRequest questionQueryRequest = new QuestionQueryRequest();
            questionQueryRequest.setBankId(questionBankQueryRequest.getId());
            questionQueryRequest.setCurrent(questionBankQueryRequest.getCurrent());
            questionQueryRequest.setPageSize(questionBankQueryRequest.getPageSize());
            Page<QuestionVO> questionPage = questionService.listQuestionByPage(questionQueryRequest);
            questionBankVO.setQuestionPage(questionPage);
        }
        //设置缓存，会根据key是否是热key进行设置
        JdHotKeyStore.smartSet(key,questionBankVO);
        return ResultUtils.success(questionBankVO);
    }

    @GetMapping("/page/vo")
    @SaCheckLogin
    public BaseResponse<Page<QuestionBankVO>> listQuestionBankPageVO(PageRequest pageRequest, HttpServletRequest request) {
        if (pageRequest == null)
            return ResultUtils.error(ErrorCode.PARAMS_ERROR);
        String remoteAddr = request.getRemoteAddr();
        Entry entry = null;
//        BaseResponse<Page<QuestionBankVO> > result = null;
        try {
           entry = SphU.entry("listQuestionBankVOPage", EntryType.IN,1, remoteAddr);
           Page<QuestionBankVO> questionBankPage = questionBankService.listQuestionBankPageVO(pageRequest, request);
           return ResultUtils.success(questionBankPage);
        }catch (Throwable ex){
            if (!BlockException.isBlockException(ex)){
                Tracer.trace(ex);
                return ResultUtils.error(ErrorCode.OPERATION_ERROR);
            }
            if (ex instanceof DegradeException)
                return FallbackHandler.listQuestionBankPageFallHandler(pageRequest,request,ex);
            return ResultUtils.error(ErrorCode.SYSTEM_ERROR,"访问过于频繁，请稍后再试");
        } finally {
            if (entry != null)
                entry.exit(1,remoteAddr);
        }
    }

    @PostMapping("/add")
    @SaCheckRole ("admin")
    public BaseResponse<QuestionBankVO> addQuestionBank(@RequestBody QuestionBankAddRequest addRequest, HttpServletRequest request) {
        if (addRequest == null)
            return ResultUtils.error(ErrorCode.PARAMS_ERROR);
        return questionBankService.addQuestionBank(addRequest,request);
    }
    @PostMapping("/update")
    @SaCheckRole ("admin")
    public BaseResponse<Boolean> updateQuestionBank(@RequestBody QuestionBankUpdateRequest updateRequest,HttpServletRequest request) {
        if (updateRequest == null || updateRequest.getId() == null)
            return ResultUtils.error(ErrorCode.PARAMS_ERROR);
        return questionBankService.updateQuestionBank(updateRequest,request);
    }
    @PostMapping("/delete")
    @SaCheckRole ("admin")
    public BaseResponse<Boolean> deleteQuestionBank(@RequestBody DeleteRequest deleteRequest) {
        if (deleteRequest == null || deleteRequest.getId() == null)
            return ResultUtils.error(ErrorCode.PARAMS_ERROR);
        return questionBankService.deleteQuestionBank(deleteRequest);
    }

}
